From 70eecd3daf07cf0b25d574d5164c4e361d88d51b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 3 Feb 2015 16:55:07 -0800 Subject: [PATCH] Support update --precise for registry deps Closes #1105 --- src/cargo/ops/cargo_generate_lockfile.rs | 10 ++++++- src/cargo/sources/registry.rs | 34 ++++++++++++++++++------ tests/test_cargo_registry.rs | 22 ++++++++++++++- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 91de74327..6b3f64d3d 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -60,8 +60,16 @@ pub fn update_lockfile(manifest_path: &Path, to_avoid.insert(dep); match opts.precise { Some(precise) => { + // TODO: see comment in `resolve.rs` as well, but this + // seems like a pretty hokey reason to single out + // the registry as well. + let precise = if dep.get_source_id().is_registry() { + format!("{}={}", dep.get_name(), precise) + } else { + precise.to_string() + }; let precise = dep.get_source_id().clone() - .with_precise(Some(precise.to_string())); + .with_precise(Some(precise)); try!(registry.add_sources(&[precise])); } None => {} diff --git a/src/cargo/sources/registry.rs b/src/cargo/sources/registry.rs index d3e311cf1..922519547 100644 --- a/src/cargo/sources/registry.rs +++ b/src/cargo/sources/registry.rs @@ -163,10 +163,10 @@ use std::io::fs::PathExtensions; use std::collections::HashMap; use curl::http; -use git2; use flate2::reader::GzDecoder; -use rustc_serialize::json; +use git2; use rustc_serialize::hex::ToHex; +use rustc_serialize::json; use tar::Archive; use url::Url; @@ -477,10 +477,26 @@ impl<'a, 'b> Registry for RegistrySource<'a, 'b> { } } - let summaries = try!(self.summaries(dep.get_name())); - let mut summaries = summaries.iter().filter(|&&(_, yanked)| { - dep.get_source_id().get_precise().is_some() || !yanked - }).map(|&(ref s, _)| s.clone()).collect::>(); + let mut summaries = { + let summaries = try!(self.summaries(dep.get_name())); + summaries.iter().filter(|&&(_, yanked)| { + dep.get_source_id().get_precise().is_some() || !yanked + }).map(|s| s.0.clone()).collect::>() + }; + + // Handle `cargo update --precise` here. If specified, our own source + // will have a precise version listed of the form `=` where + // `` is the name of a crate on this source and `` is the + // version requested (agument to `--precise`). + summaries.retain(|s| { + match self.source_id.get_precise() { + Some(p) if p.starts_with(dep.get_name()) => { + let vers = &p[dep.get_name().len() + 1..]; + s.get_version().to_string() == vers + } + _ => true, + } + }); summaries.query(dep) } } @@ -491,8 +507,10 @@ impl<'a, 'b> Source for RegistrySource<'a, 'b> { // to look for, so we always atempt to perform an update here. // // If we have a precise version, then we'll update lazily during the - // querying phase. - if self.source_id.get_precise().is_none() { + // querying phase. Note that precise in this case is only + // `Some("locked")` as other `Some` values indicate a `cargo update + // --precise` request + if self.source_id.get_precise() != Some("locked") { try!(self.do_update()); } Ok(()) diff --git a/tests/test_cargo_registry.rs b/tests/test_cargo_registry.rs index 2873d2ee3..3d1296c3b 100644 --- a/tests/test_cargo_registry.rs +++ b/tests/test_cargo_registry.rs @@ -462,23 +462,43 @@ test!(update_lockfile { .file("src/main.rs", "fn main() {}"); p.build(); + println!("0.0.1"); r::mock_pkg("bar", "0.0.1", &[]); assert_that(p.process(cargo_dir().join("cargo")).arg("build"), execs().with_status(0)); r::mock_pkg("bar", "0.0.2", &[]); + r::mock_pkg("bar", "0.0.3", &[]); fs::rmdir_recursive(&paths::home().join(".cargo/registry")).unwrap(); + println!("0.0.2 update"); assert_that(p.process(cargo_dir().join("cargo")).arg("update") - .arg("-p").arg("bar"), + .arg("-p").arg("bar").arg("--precise").arg("0.0.2"), execs().with_status(0).with_stdout(format!("\ {updating} registry `[..]` ", updating = UPDATING).as_slice())); + println!("0.0.2 build"); assert_that(p.process(cargo_dir().join("cargo")).arg("build"), execs().with_status(0).with_stdout(format!("\ {downloading} [..] v0.0.2 (registry file://[..]) {compiling} bar v0.0.2 (registry file://[..]) {compiling} foo v0.0.1 ({dir}) +", downloading = DOWNLOADING, compiling = COMPILING, + dir = p.url()).as_slice())); + + println!("0.0.3 update"); + assert_that(p.process(cargo_dir().join("cargo")).arg("update") + .arg("-p").arg("bar"), + execs().with_status(0).with_stdout(format!("\ +{updating} registry `[..]` +", updating = UPDATING).as_slice())); + + println!("0.0.3 build"); + assert_that(p.process(cargo_dir().join("cargo")).arg("build"), + execs().with_status(0).with_stdout(format!("\ +{downloading} [..] v0.0.3 (registry file://[..]) +{compiling} bar v0.0.3 (registry file://[..]) +{compiling} foo v0.0.1 ({dir}) ", downloading = DOWNLOADING, compiling = COMPILING, dir = p.url()).as_slice())); }); -- 2.30.2